<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <h1 data-lake-id="B0q5f" id="B0q5f"><span data-lake-id="u5752c8e5" id="u5752c8e5">典型回答</span></h1>
  <p data-lake-id="uc00338da" id="uc00338da"><br></p>
  <p data-lake-id="udb46a4ce" id="udb46a4ce"><span data-lake-id="u490cdcb3" id="u490cdcb3">Feign是Spring Cloud中的一个声明式的HTTP客户端库，用于简化编写基于HTTP的服务调用代码。但是从Spring Cloud 2020版本开始，官方宣布Feign将不再维护和支持，推荐使用OpenFeign作为替代方案。</span></p>
  <p data-lake-id="ua9cb471b" id="ua9cb471b"><span data-lake-id="u70da7937" id="u70da7937">​</span><br></p>
  <p data-lake-id="u83c9c1fe" id="u83c9c1fe"><span data-lake-id="u75757b4e" id="u75757b4e">但是，随着SpringCloud 2022的发布，官方宣布OpenFeign将被视为功能完整。这意味着Spring Cloud团队将不再向模块添加新特性。只会修复bug和安全问题。</span></p>
  <p data-lake-id="u3142962e" id="u3142962e"><span data-lake-id="u511a9c16" id="u511a9c16">​</span><br></p>
  <p data-lake-id="u38ecb283" id="u38ecb283"><span data-lake-id="u54700a91" id="u54700a91">但是，在Spring 6.0 发布之后，Spring内置了一个HTTP客户端——@HttpExchange （</span><a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/service/annotation/HttpExchange.html" target="_blank" data-lake-id="u5f230677" id="u5f230677"><span data-lake-id="u010be347" id="u010be347">https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/service/annotation/HttpExchange.html</span></a><span data-lake-id="ud4f4c68c" id="ud4f4c68c"> ），建议大家使用这个客户端进行HTTP调用。</span></p>
  <p data-lake-id="uc6f15650" id="uc6f15650"><span data-lake-id="uf6ef1044" id="uf6ef1044">​</span><br></p>
  <h1 data-lake-id="nAozn" id="nAozn"><span data-lake-id="u44c504a1" id="u44c504a1">扩展知识</span></h1>
  <p data-lake-id="u20cf874a" id="u20cf874a"><br></p>
  <h2 data-lake-id="xa8CN" id="xa8CN"><span data-lake-id="u2ef3cb56" id="u2ef3cb56">@HttpExchange使用</span></h2>
  <p data-lake-id="u9c23a239" id="u9c23a239"><br></p>
  <p data-lake-id="ueb144d0f" id="ueb144d0f"><span data-lake-id="uab71b47f" id="uab71b47f">想要使用这个新的HTTP客户端，需要Spring升级到6.0，或者SpringBoot升级到3.0版本，然后再POM中依赖spring-web</span></p>
  <p data-lake-id="u2f2face4" id="u2f2face4"><span data-lake-id="u37d98fc7" id="u37d98fc7">​</span><br></p>
  <pre lang="java"><code>
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
  <p data-lake-id="u64257bcb" id="u64257bcb"><span data-lake-id="u0a2a8e5c" id="u0a2a8e5c">​</span><br></p>
  <p data-lake-id="u726785ad" id="u726785ad"><span data-lake-id="uef8316b7" id="uef8316b7" style="color: rgb(18, 18, 18)">在 Spring 6.0中，可以让HTTP 服务接口带有</span><span data-lake-id="u586d920a" id="u586d920a" style="color: rgb(18, 18, 18); background-color: rgb(246, 246, 246)">@HttpExchange</span><span data-lake-id="ub46a71c1" id="ub46a71c1" style="color: rgb(18, 18, 18)">。那么这个接口方法就会被视为 HTTP 端点，目前支持的注解有以下几个：</span></p>
  <p data-lake-id="u34a02dc2" id="u34a02dc2"><br></p>
  <ul list="ub61f30fa">
   <li fid="u5c7bd718" data-lake-id="u6cf6cb44" id="u6cf6cb44"><span data-lake-id="uf0ca0c11" id="uf0ca0c11">@GetExchange HTTP GET 请求</span></li>
   <li fid="u5c7bd718" data-lake-id="u09aba68c" id="u09aba68c"><span data-lake-id="uea4f5c90" id="uea4f5c90">@PostExchange HTTP POST 请求</span></li>
   <li fid="u5c7bd718" data-lake-id="u8d77c1d8" id="u8d77c1d8"><span data-lake-id="ue39859c4" id="ue39859c4">@PutExchange HTTP PUT 请求</span></li>
   <li fid="u5c7bd718" data-lake-id="ude59b3ea" id="ude59b3ea"><span data-lake-id="uf1174200" id="uf1174200">@PatchExchange HTTP PATCH 请求</span></li>
   <li fid="u5c7bd718" data-lake-id="uc673bab1" id="uc673bab1"><span data-lake-id="ud87ff62d" id="ud87ff62d">@DeleteExchange HTTP DELETE 请求</span></li>
  </ul>
  <p data-lake-id="u15e5cc1f" id="u15e5cc1f"><span data-lake-id="u9c267785" id="u9c267785">​</span><br></p>
  <p data-lake-id="uc2849a7d" id="uc2849a7d"><span data-lake-id="ua81dac5f" id="ua81dac5f">首先，我们自己定义一个HTTP接口，定义一个实体类：</span></p>
  <p data-lake-id="u2d044812" id="u2d044812"><span data-lake-id="ua08596da" id="ua08596da">​</span><br></p>
  <pre lang="java"><code>
public class User implements Serializable {

    private String name;
    private Integer age;
    // Constructor、Getter、Setter
    @Override
    public String toString() {
        return name + ":" + age;
    }
}

</code></pre>
  <p data-lake-id="u87644a70" id="u87644a70"><span data-lake-id="u15d41759" id="u15d41759">​</span><br></p>
  <p data-lake-id="u590de23a" id="u590de23a"><span data-lake-id="u79d276f6" id="u79d276f6">然后定义一个Controller</span></p>
  <pre lang="java"><code>
@GetMapping("/users")
public List&lt;User&gt; list() {
    return IntStream.rangeClosed(20, 25)
            .mapToObj(i -&gt; new User("Hollis",  i))
            .collect(Collectors.toList());
}
</code></pre>
  <p data-lake-id="u1ad8055b" id="u1ad8055b"><span data-lake-id="u5c191df8" id="u5c191df8">​</span><br></p>
  <p data-lake-id="uae9623a2" id="uae9623a2"><span data-lake-id="u6f986312" id="u6f986312">以上，服务在启动后，通过</span><code data-lake-id="u939e5bbe" id="u939e5bbe"><span data-lake-id="u4f130fc7" id="u4f130fc7">http://localhost:8080/users</span></code><span data-lake-id="uad6c237c" id="uad6c237c">地址访问后会得到10个我生成的用户信息。</span></p>
  <p data-lake-id="ucce2d173" id="ucce2d173"><span data-lake-id="u92603ae3" id="u92603ae3">​</span><br></p>
  <p data-lake-id="ua63fbcf8" id="ua63fbcf8"><span data-lake-id="u1530727b" id="u1530727b">有了一个HTTP接口之后，用</span><span data-lake-id="u773a4374" id="u773a4374" style="color: rgb(18, 18, 18); background-color: rgb(246, 246, 246)">@HttpExchange </span><span data-lake-id="uef6e1c0c" id="uef6e1c0c">调用方式如下：</span></p>
  <p data-lake-id="u178020f3" id="u178020f3"><span data-lake-id="ufe7eb8c2" id="ufe7eb8c2">​</span><br></p>
  <pre lang="java"><code>
public interface UserApiService {
    @GetExchange("/users")
    List&lt;User&gt; getUsers();
}
</code></pre>
  <p data-lake-id="u3f5df975" id="u3f5df975"><span data-lake-id="u258a782b" id="u258a782b" style="color: rgb(18, 18, 18)">​</span><br></p>
  <p data-lake-id="u338f74e6" id="u338f74e6"><span data-lake-id="u0a1de33f" id="u0a1de33f" style="color: rgb(18, 18, 18)">还需要定义一个用于HTTP调用的client：</span></p>
  <p data-lake-id="u4a866df7" id="u4a866df7"><span data-lake-id="uc5696411" id="uc5696411" style="color: rgb(18, 18, 18)">​</span><br></p>
  <pre lang="java"><code>
@Configuration
public class WebConfiguration {

    @Bean
    public WebClient  webClient() {
        return WebClient.builder()
                                .baseUrl("https://localhost:8080")
                                .build();
    }

    @Bean
    UserApiService userApiService(){
        HttpServiceProxyFactory httpServiceProxyFactory =
                HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient()))
                        .build();
        return httpServiceProxyFactory.createClient(UserApiService.class);
    }
    	
}
</code></pre>
  <p data-lake-id="u3c628861" id="u3c628861"><span data-lake-id="ue9f64d8d" id="ue9f64d8d" style="color: rgb(18, 18, 18)">​</span><br></p>
  <p data-lake-id="u99bb83a0" id="u99bb83a0"><span data-lake-id="u6b6b6937" id="u6b6b6937" style="color: rgb(18, 18, 18)">然后就可以调用了，如：</span></p>
  <p data-lake-id="u42836f73" id="u42836f73"><span data-lake-id="uc707523b" id="uc707523b" style="color: rgb(18, 18, 18)">​</span><br></p>
  <pre lang="java"><code>
@SpringBootTest
class UsersTests {

    @Autowired
    private UserApiService userApiService;

    @Test
    public void testGetUsers(){
       List&lt;User&gt; users = userApiService.getUsers();
       Assert.assertEquals(users.size(),10);
    }
    
}
</code></pre>
  <p data-lake-id="u253d3e6e" id="u253d3e6e"><span data-lake-id="u6c3abd1d" id="u6c3abd1d" style="color: rgb(18, 18, 18)">​</span><br></p>
 </body>
</html>